package com.lei.zjs.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.lei.zjs.service.ebi.UserService;
import com.lei.zjs.utils.BOSContext;
import com.lei.zjs.utils.MD5Utils;
import com.lei.zjs.vo.User;
import com.lei.zjs.web.action.base.BaseAction;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {

	@Resource
	private UserService userService;

	private String checkcode;

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	// Shiro用户登录(用户认证)
	public String login() {
		String validateCode = (String) ServletActionContext.getRequest()
				.getSession().getAttribute("key");
		if (StringUtils.isBlank(checkcode)
				|| !checkcode.equalsIgnoreCase(validateCode)) {
			// 验证码错误
			this.addActionError(this.getText("checkCodeError"));
			return "login";
		} else {
			// 验证码通过
			Subject subject = SecurityUtils.getSubject();
			String username = model.getUsername();
			String password = model.getPassword();
			password = MD5Utils.md5(password);
			AuthenticationToken token = new UsernamePasswordToken(username, password);
			
			try {
				subject.login(token);
				//将user存入session中
				User user = (User) subject.getPrincipal();
				ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
				return "home";
			} catch (UnknownAccountException e) {
				e.printStackTrace();
				this.addActionError("当前用户名不存在!");
				return "login";
			} catch (IncorrectCredentialsException e){
				e.printStackTrace();
				this.addActionError("当前输入的密码错误!");
				return "login";
			}
		}
	}
	
	// 传统用户登录
		public String login_bak() {
			String validateCode = (String) ServletActionContext.getRequest()
					.getSession().getAttribute("key");
			if (StringUtils.isBlank(checkcode)
					|| !checkcode.equalsIgnoreCase(validateCode)) {
				// 验证码错误
				this.addActionError(this.getText("checkCodeError"));
				return "login";
			} else {
				// 验证码通过
				User user = userService.login(model);
				if (user != null) {
					// 登录成功
					ServletActionContext.getRequest().getSession()
							.setAttribute("loginUser", user);
					return "home";
				} else {
					// 登录失败
					this.addActionError(this.getText("loginFail"));
					return "login";
				}
			}
		}

	// 用户注销
	public String logout() {
		ServletActionContext.getRequest().getSession().invalidate();
		return "login";
	}
	
	private String oldPass;
	
	public void setOldPass(String oldPass) {
		this.oldPass = oldPass;
	}

	//修改用户密码
	public String editPassword() throws IOException{
		User loginUser = BOSContext.getLoginUser();
		String id = loginUser.getId();
		String currPassword = loginUser.getPassword();
		String flag = "-1";
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		if (!currPassword.equals(MD5Utils.md5(oldPass))) {
			ServletActionContext.getResponse().getWriter().print(flag);
			return NONE;
		}
		try {
			userService.editPassword(id,model.getPassword());
			flag = "1";
		} catch (Exception e) {
			flag = "0";
			e.printStackTrace();
		}
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		writer.print(flag);
		return NONE;
	}
	
	//查询所有用户
	public String list() throws IOException{
		List<User> list = userService.findAll();
		
		String[] excludes = new String[]{"noticebills","roles"};
		this.writeJsonArray2String(list, excludes);
		return NONE;
	}
	
	
	private String[] roleIds;
	
	public void setRoleIds(String[] roleIds) {
		this.roleIds = roleIds;
	}

	//保存用户
	public String save(){
		userService.save(model,roleIds);
		return "list";
	}
}
